const {
    Controller
} = require('egg');
class BaseController extends Controller {
    success(data) {
        if (!data.affectedRows) {
            // 为空说明没有修改数据库，再判断是否为字符串
            if (typeof (data) === 'string') {
                // 为字符串说明为报错信息
                this.ctx.body = {
                    success: false,
                    message: data,
                };
            } else {
                // 不为字符串则为数据，直接返回data
                this.ctx.body = {
                    success: true,
                    data,
                };
            }
        } else {
            // 判断修改的条数是否大于等于1
            if (data.affectedRows >= 1) {
                this.ctx.body = {
                    success: true,
                    message: "操作成功",
                };
            } else {
                this.ctx.body = {
                    success: false,
                    message: "操作失败"
                };
            }
        }
    }
    error(message) {
        this.ctx.body = {
            success: false,
            message: message
        }
    }
    async user(str) {
        const {
            ctx,
            app
        } = this;
        try {
            const res = await ctx.service.user[str]();
            this.success(res)
        } catch (error) {
            this.error(error.message)
        }
    }
    async article(str) {
        const {
            ctx,
            app
        } = this;
        try {
            const res = await ctx.service.article[str]();
            this.success(res)
        } catch (error) {
            this.error(error.message)
        }
    }
    async page(str) {
        const {
            ctx,
            app
        } = this;
        try {
            const res = await ctx.service.page[str]();
            this.success(res)
        } catch (error) {
            this.error(error.message)
        }
    }
    async view(str) {
        const {
            ctx,
            app
        } = this;
        try {
            const res = await ctx.service.view[str]();
            this.success(res)
        } catch (error) {
            this.error(error.message)
        }
    }
    async search(str) {
        const {
            ctx,
            app
        } = this;
        try {
            const res = await ctx.service.search[str]();
            this.success(res)
        } catch (error) {
            this.error(error.message)
        }
    }
    async email(str) {
        const {
            ctx,
            app
        } = this;
        try {
            const res = await ctx.service.email[str]();
            this.success(res)
        } catch (error) {
            this.error(error.message)
        }
    }
    async file(str) {
        const {
            ctx,
            app
        } = this;
        try {
            const res = await ctx.service.file[str]();
            this.success(res)
        } catch (error) {
            this.error(error.message)
        }
    }
    async comment(str) {
        const {
            ctx,
            app
        } = this;
        try {
            const res = await ctx.service.comment[str]();
            this.success(res)
        } catch (error) {
            this.error(error.message)
        }
    }
    async knowledge(str) {
        const {
            ctx,
            app
        } = this;
        try {
            const res = await ctx.service.knowledge[str]();
            this.success(res)
        } catch (error) {
            this.error(error.message)
        }
    }
    async pay(str) {
        const {
            ctx,
            app
        } = this;
        try {
            const res = await ctx.service.pay[str]();
            this.success(res)
        } catch (error) {
            this.error(error.message)
        }
    }
}
module.exports = BaseController;